package www.ym.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.ym.com.common.CustomException;
import www.ym.com.dto.SetmealDto;
import www.ym.com.entity.Category;
import www.ym.com.entity.Setmeal;
import www.ym.com.entity.SetmealDish;
import www.ym.com.mapper.SetmealMapper;
import www.ym.com.service.CategoryService;
import www.ym.com.service.SetmealDishService;
import www.ym.com.service.SetmealService;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐并保存菜品信息
     *
     * @param setmealDto
     */
    @Transactional
    @Override
    public void addWidthDish(SetmealDto setmealDto) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getName, setmealDto.getName());
        if (setmealService.count(wrapper) > 0) {
            throw new CustomException("套餐已存在");
        }
        setmealDto.setStatus(1);
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }


    /**
     * 修改套餐包括菜品
     *
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {


        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getName, setmealDto.getName());
        if (setmealService.count(wrapper) > 1) {
            throw new CustomException("套餐已存在");
        }
        this.updateById(setmealDto);

        //删除相关联的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes =setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐和相关联的菜品
     *
     * @param id
     */
    @Override
    public void delById(Long id) {
        Setmeal byId = setmealService.getById(id);
        if (byId.getStatus() == 1) {
            throw new CustomException("起售状态的套餐无法删除");
        }
        setmealService.removeById(id);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        setmealDishService.remove(wrapper);
    }

    /**
     * 批量、单个修改售卖状态
     *
     * @param status
     * @param ids
     */
    @Override
    public void changeStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ids != null, Setmeal::getId, ids);
        List<Setmeal> list = setmealService.list(wrapper);
        for (Setmeal item : list) {
            item.setStatus(status);
            setmealService.updateById(item);
        }
    }

    /**
     * 详情 ,通过套餐查询菜品信息
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdDish(Long id) {
        Setmeal byId = setmealService.getById(id);
        SetmealDto dto = new SetmealDto();
        BeanUtils.copyProperties(byId, dto);

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId,byId.getCategoryId());
        Category category = categoryService.getOne(queryWrapper);
        dto.setCategoryName(category.getCategoryName());

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = this.setmealDishService.list(wrapper);
        dto.setSetmealDishes(list);

        return dto;
    }


//    /**
//     * 新增套餐
//     *
//     * @param
//     */
//    @Transactional
//    @Override
//    public void saveWithDish(SetmealDto setmealDto) {
//
//        //保存套餐基本信息
//        this.save(setmealDto);
//        //套餐id
//        Long id = setmealDto.getId();
//
//        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
//        dishes = dishes.stream().map(item -> {
//            item.setSetmealId(id);
//            return item;
//        }).collect(Collectors.toList());
//        setmealDishService.saveBatch(dishes);
//    }
//
//    //根据id查询套餐信息和对应的菜品信息
//    @Override
//    public SetmealDto getByIdDish(Long id) {
//        //查询套餐基本信息保存至SetmealDto
//        Setmeal setmeal = this.getById(id);
//        SetmealDto setmealDto = new SetmealDto();
//        BeanUtils.copyProperties(setmeal, setmealDto);
//
//        //查询套餐的菜品信息保存至setmealDishes
//        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(SetmealDish::getSetmealId, id);
//        List<SetmealDish> list = setmealDishService.list(queryWrapper);
//        setmealDto.setSetmealDishes(list);
//        return setmealDto;
//    }
//
//    /**
//     * 修改套餐信息
//     * @param setmealDish
//     */
//    @Transactional
//    @Override
//    public void updateWithDish(SetmealDto setmealDish) {
//        //保存套餐基本信息
//        this.updateById(setmealDish);
//
//        //删除原有的菜品信息
//        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(SetmealDish::getSetmealId, setmealDish.getId());
//        setmealDishService.remove(queryWrapper);
//
//        //保存客户提交的信息至数据库
//        List<SetmealDish> list = setmealDish.getSetmealDishes();
//        list = list.stream().map((item) -> {
//            item.setSetmealId(setmealDish.getId());
//            return item;
//        }).collect(Collectors.toList());
//        setmealDishService.saveBatch(list);
//    }
//
//    @Override
//    public void deleteWithDish(List<Long> ids) {
//
//        //判断是否可以删除，售卖状态不可删除
//       LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
//       queryWrapper.in(ids!=null,Setmeal::getId,ids);
//       queryWrapper.eq(Setmeal::getStatus,1);
//       int count = this.count(queryWrapper);
//
//       if (count>0){
//           throw new CustomException("套餐正在售卖中，无法删除");
//       }
//       this.removeByIds(ids);
//
//       //删除Setmealdish中的关联数据
//        LambdaQueryWrapper<SetmealDish> queryWrapper1=new LambdaQueryWrapper<>();
//        queryWrapper1.in(SetmealDish::getSetmealId,ids);
//        setmealDishService.remove(queryWrapper1);
//    }
}